package com.comut.crud;

import com.comut.crud.ejb.interfaces.remote.IDriverActionRemote;
import com.comut.crud.pojo.Response;
import com.comut.crud.pojo.mobile.DriverInfo;
import java.util.ResourceBundle;
import javax.naming.InitialContext;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import org.apache.log4j.Logger;

/**
 * REST Web Service Issue: IFL-1569
 *
 * @author Vitor Borrego
 */
@Path("driversForUserId/{userId}")
public class DriversForUserIdResource {

    private static Logger _logger = org.apache.log4j.Logger.getLogger(DriversForUserIdResource.class.getName());
    final String jndiName = ResourceBundle.getBundle("jndi").getString("driverBeanRemote");

    /**
     * Gets a list of drivers for a userId. Issue: IFL-1569
     */
    @GET
    @Produces("application/json; charset=UTF-8")
    public Response getJson(@PathParam("userId") int userId) {
        //http://localhost:8080/CRUD-EA-web/resources/driversForUserId/332
        Response response = new Response();

        try {            
            _logger.info("GET STARTED");
            javax.naming.Context ic = new InitialContext();
            IDriverActionRemote driverAction = (IDriverActionRemote) ic.lookup(jndiName);
            DriverInfo[] driverInfoArray = driverAction.getListDrivers(userId);
            _logger.info("GET DONE");
            response.setRetVal(driverInfoArray);
            response.setSuccess(true);
        } catch (Exception ex) {
            _logger.info(ex.getMessage());
            response.setSuccess(false);
            response.setMessageError(ex.getMessage());
        }

        return response;
    }
}
